<?php

namespace app\common\model;

use think\Model;
/**
 * 邮件
 * 字段：
 * mail_id:邮件的标识符
 * mail_type：邮件的种类，toadmin:用户发送给管理，reply：管理员回复用户
 * title：邮件标题
 * content：邮件内容
 * brief:简介
 * is_read：是否已读
 * send_time:发送时间
 * is_deleted_in_inbox:在收件箱被删除
 * is_deleted_in_outbox：在发件箱被删除
 * 外部关系:
 * target_user：主要关系用户
 * send_user:具体发件人
 */
class Mail extends Model
{
    protected $insert = array(
      'is_read' => 0,
      'send_time' => '',
    );
    public function getMailTypeAttr($value)
    {
        $data = ['toadmin', 'reply'];

        return $data[$value];
    }
    public function setMailTypeAttr($value)
    {
        $data = ['toadmin' => 0, 'reply' => 1];

        return $data[$value];
    }
    public function setSendTimeAttr($value)
    {
        if (empty($value)) {
            return date("Y-m-d H:i:s");
        } else {
            return $value;
        }
    }
    public function getBriefAttr($value, $data)
    {
        $brief = $data['content'];
        $brief = strip_tags($brief);
        $brief = trim($brief);
        if (strlen($brief) > 48) {
            $brief = substr($brief, 0, 44).'...';
        }

        return $brief;
    }
    public function send_user()
    {
        return $this->belongsTo('User', 'send_user_id', 'user_id');
    }
    public function target_user()
    {
        return $this->belongsTo('User', 'target_user_id', 'user_id');
    }
    public function getIsDeletedInInboxAttr($value,$data)
    {
      return $data['mail_status']%2;
    }
    public function getIsDeletedInOutboxAttr($value,$data)
    {
      return $data['mail_status']>2?1:0;
    }
}
